package com.zmovie.front.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.service.*;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.MovieVO;
import com.zmovie.front.util.DateUtil;
import com.zmovie.front.util.MoviePathUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.front.controller
 * @since 2023-12-15 22:09
 */
@Controller
@RequestMapping("/movie")
@Api(tags = "电影页面")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieToTypeService movieToTypeService;
    @Autowired
    private MovieTypeService movieTypeService;
    @Autowired
    private MovieAgeService movieAgeService;
    @Autowired
    private MovieAreaService movieAreaService;
    @Autowired
    private MovieRateService movieRateService;

    private void renderData(Model model, Integer showType, Integer typeId,
                            Movie movie, Integer pageNo, Integer pageSize) {
        //电影分页
        Page<Movie> moviePage = movieService.pageMovie(movie, pageNo, pageSize);
        List<Movie> movies = moviePage.getRecords();
        //分页函数里没有考虑电影类型和showType
        movies = movies.stream().filter(new Predicate<Movie>() {
            @Override
            public boolean test(Movie movie) {
                //showType: 1-正在热映 2-即将上映 3-经典电影
                switch (showType) {
                    case 1:
                        //正在热映-当前时间前2月到目前
                        if (DateUtil.afterNow(movie.getReleaseDate()) || DateUtil.beforeNow(DateUtil.addMonth(movie.getReleaseDate(), 2))) {
                            return false;
                        }
                        break;
                    case 2:
                        //即将上映-大于当前日期的
                        if (DateUtil.beforeNow(movie.getReleaseDate())) {
                            return false;
                        }
                        break;
                    default:
                        //经典电影-小于当期日期的
                        if (DateUtil.afterNow(movie.getReleaseDate())) {
                            return false;
                        }
                        break;
                }
                if (typeId != null){
                    //保留绑定该类型的电影
                    List<MovieToType> movieToTypes = movieToTypeService.listByMovieId(movie.getId());
                    for(MovieToType movieToType : movieToTypes){
                        if(typeId.equals(movieToType.getTypeId())){
                            return true;
                        }
                    }
                    return false;
                }
                return true;
            }
        }).collect(Collectors.toList());
        moviePage.setRecords(movies);
        model.addAttribute(Constants.MOVIE_PAGE_KEY, convert(moviePage));
        //前端选项
        model.addAttribute(Constants.SHOW_TYPE_KEY, showType);
        model.addAttribute(Constants.SHOW_AREA_ID_KEY, movie.getAreaId() == null ? 0 : movie.getAreaId());
        model.addAttribute(Constants.SHOW_AGE_ID_KEY, movie.getAgeId() == null ? 0 : movie.getAgeId());
        model.addAttribute(Constants.SHOW_TYPE_ID_KEY, typeId == null ? 0 : typeId);
    }

    @GetMapping
    @ApiOperation("电影页面")
    public String index(Model model, Movie movie,
                        @RequestParam(required = false, defaultValue = "1") Integer showType) {
        //所有电影类型
        List<MovieType> movieTypes = movieTypeService.listMovieTypes(new MovieType());
        model.addAttribute(Constants.MOVIE_TYPES_KEY, movieTypes);
        //所有电影年代
        List<MovieAge> movieAges = movieAgeService.listMovieAge(new MovieAge());
        model.addAttribute(Constants.MOVIE_AGES_KEY, movieAges);
        //所有电影区域
        List<MovieArea> movieAreas = movieAreaService.listMovieArea(new MovieArea());
        model.addAttribute(Constants.MOVIE_AREAS_KEY, movieAreas);
        //渲染数据
        renderData(model, showType, null, movie, Constants.PAGE_NO, Constants.PAGE_SIZE);
        return "movies";
    }

    @PostMapping
    @ApiOperation("电影分页片段")
    public String pageMovie(Model model,
                            @RequestParam(required = false, defaultValue = "1") Integer showType,
                            @RequestParam(required = false) Integer typeId,
                            Movie movie,
                            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        //渲染数据
        renderData(model, showType, typeId, movie, pageNo, pageSize);
        //返回电影分页
        return "movies :: movieList";
    }

    private Page<MovieVO> convert(Page<Movie> moviePage) {
        Page<MovieVO> movieVOPage = new Page<>();
        BeanUtils.copyProperties(moviePage, movieVOPage);
        movieVOPage.setRecords(convert(moviePage.getRecords()));
        return movieVOPage;
    }

    private List<MovieVO> convert(List<Movie> movies) {
        return movies.stream().map(this::convert).collect(Collectors.toList());
    }

    private MovieVO convert(Movie movie) {
        //处理文件路径
        MovieVO movieVO = MoviePathUtil.dealMovieFilePath(movie);
        //评分
        MovieRate movieRate = movieRateService.getByMovieId(movie.getId());
        movieVO.setMovieRate(movieRate);
        return movieVO;
    }
}
